import {
  IUploadFileOptions,
  httpDelete,
  httpGet,
  httpPost,
  httpPut,
  uploadFile
} from "@/utils/request"
import { IContentType } from "@/utils/enum"
import { withRequestCache } from "@/utils/app"
import { querySerialize } from "@/utils/common"

export const wechatLoginPath = "/login/login"
/**
 * 微信登录
 * @param code 用户登录凭证（有效期五分钟）。开发者需要在开发者服务器后台调用 auth.code2Session，使用 code 换取 openid 和 session_key 等信息
 */
export const wechatLogin = (code: string) =>
  httpPost<IApiRelative.IWechatLogin.IResData>(
    wechatLoginPath,
    { code },
    {
      showLoading: true,
      header: {
        "content-type": IContentType.json
      }
    }
  )
/** 小程序首页数据 */
export const getIndexData = () => httpGet<IApiRelative.IGetIndexData.IResData>("/home/data")
/** 小程序当前登录的用户信息 */
export const getCurrentUserInfo = (param?: IApiRelative.IGetCurrentUserInfo.IParams) =>
  httpGet<IUserInfo>("/my/info", param)
/** 退出登录 */
export const logout = () => httpPost<unknown>("/my/logout")
/** 注销并清除用户信息 */
export const logoff = () => httpPost<unknown>("/my/logoff")
/** 小程序用户资料更新 */
export const updateUserInfo = (params: IApiRelative.IUpdateUserInfo.IParams) =>
  httpPut<unknown>("/my/info", params, {
    header: {
      "content-type": IContentType.json
    }
  })
/** 培训项目选项
 * @description 综合选项 值为3
 */
export const getProjectOptions = () => httpGet<IOptionLike<number>[]>("/home/projects")
/** 省市选项
 * @description 仅第一次请求后台数据, 请求后保存在内存中, 下次请求直接读取内存
 */
export const getAreaOptions = withRequestCache<IApiRelative.IGetAreaOptions.IResData>(
  () => httpGet<IApiRelative.IGetAreaOptions.IResData>("/home/areas"),
  "/home/areas"
)
/** 获取手机号 */
export const getUserPhoneNumber = (params: IApiRelative.IGetUserPhoneNumber.IParams) =>
  httpPost<IApiRelative.IGetUserPhoneNumber.IResData>("/login/auto-login", params, {
    header: {
      "content-type": IContentType.json
    }
  })
/** 上传图片/视频 */
export const uploadV4 = (filePath, name?: string, options?: IUploadFileOptions) =>
  uploadFile("/upload/v4", filePath, name, options)
/** 获取小程序频道列表 */
export const getChannelList = () =>
  httpGet<IApiRelative.IGetChannelList.IResData>("/community/channels")
/** 获取小程序频道笔记列表 */
export const getChannelNotes = (params: IApiRelative.IGetChannelNotes.IParams) =>
  httpPost<IApiRelative.IGetChannelNotes.IResData>("/community/notes", params, {
    header: {
      "content-type": IContentType.json
    }
  })
/** 笔记详情 */
export const GetNoteDetail = (
  params: /** 笔记id */ PrimaryKey<number>,
  data?: { /** 关联的话题id */ discussionId: number }
) =>
  httpGet<IApiRelative.IGetNoteDetail.IResData>(
    `/community/note/${params.id}` + querySerialize(data as any, true)
  )
/** 选择艺考年份 地区 培训项目 提交 */
export const ChooseInfoSubmit = (params: IApiRelative.IChooseInfoSubmit.IParams) =>
  httpPut<any>("/my/init", params, {
    header: {
      "content-type": IContentType.json
    }
  })
/** 搜索页 搜索机构 */
export function searchInstitution(params: IApiRelative.ISearchInstitution.IParams) {
  return httpPost<IApiRelative.ISearchInstitution.IResData>(
    "https://openapi.91goodschool.com/api/mp/schools",
    params,
    {
      header: {
        "content-type": IContentType.json
      },
      urlCustom: true
    }
  )
}
/** 搜索页 搜索资讯 */
export function searchNews(params: IApiRelative.ISearchNews.IParams) {
  return httpPost<IApiRelative.ISearchNews.IResData>("/home/search-news", params, {
    header: {
      "content-type": IContentType.json
    }
  })
}
/** 搜索页 搜索话题 */
export function searchWordTopic(params: IApiRelative.ISearchWordTopic.IParams) {
  return httpPost<IApiRelative.ISearchWordTopic.IResData>("/home/search-discussion", params, {
    header: {
      "content-type": IContentType.json
    }
  })
}
/** 搜索页 搜索笔记 */
export function searchNotes(params: IApiRelative.ISearchNotes.IParams) {
  return httpPost<IApiRelative.ISearchNotes.IResData>("/home/search-note", params, {
    header: {
      "content-type": IContentType.json
    }
  })
}
/** 话题详情 */
export function getTopicDetail(params: PrimaryKey<string>) {
  return httpGet<IApiRelative.IGetTopicDetail.IResData>("/content-discussion/" + params.id)
}
/** 话题详情页面 笔记列表 */
export function getTopicRelatedNotes(
  params: /** 话题id */ PrimaryKey<string | number>,
  data: IApiRelative.IGetTopicRelatedNotes.IParams
) {
  return httpPost<IApiRelative.IGetTopicRelatedNotes.IResData>(
    "/content-discussion/notes/" + params.id,
    data,
    {
      header: {
        "content-type": IContentType.json
      }
    }
  )
}
/** 话题广场列表 */
export function getTopicSquareList(params: IApiRelative.IGetTopicSquareList.IParams) {
  return httpPost<IApiRelative.IGetTopicSquareList.IResData>("/content-discussion/grid", params, {
    header: {
      "content-type": IContentType.json
    }
  })
}
/** 笔记点赞、取消点赞 */
export function noteLikeSwitch(params: PrimaryKey<number | string>) {
  return httpPost<IApiRelative.INoteLikeSwitch.IResData>("/my/like-note/" + params.id)
}
/** 访客关注、取消关注 */
export function concernPerson(params: { miniVisitorId: number }) {
  return httpPost<unknown>("/home/visitor-follow/" + params.miniVisitorId)
}
/** 笔记详情 点评列表 */
export function getNoteComments(
  params: IApiRelative.IGetNoteComments.IParams,
  data: IApiRelative.IGetNoteComments.IData
) {
  const query = {} as Pick<IApiRelative.IGetNoteComments.IParams, "parentId">
  if (params.parentId) {
    query.parentId = params.parentId
  }
  return httpPost<IApiRelative.IGetNoteComments.IResData>(
    "/community/note-comments/" + params.id + querySerialize(query, true),
    data,
    {
      header: {
        "content-type": IContentType.json
      }
    }
  )
}
/** 点评 删除 */
export function deleteNoteComment(params: /** 评论id*/ PrimaryKey<number | string>) {
  return httpDelete<unknown>("/my/comment/" + params.id)
}
/** 点评笔记 */
export function commentNote(
  params: /** 笔记id*/ PrimaryKey<number | string>,
  data: IApiRelative.ICommentNote.IData
) {
  return httpPost<IApiRelative.IGetNoteComments.IGetNoteCommentItem>(
    "/my/comment-note/" + params.id,
    data,
    {
      header: {
        "content-type": IContentType.json
      }
    }
  )
}
/** 点评资讯 */
export function commentNews(
  params: /** 资讯id*/ PrimaryKey<number | string>,
  data: IApiRelative.ICommentNews.IData
) {
  return httpPost<IApiRelative.IGetNoteComments.IGetNoteCommentItem>(
    "/my/comment-news/" + params.id,
    data,
    {
      header: {
        "content-type": IContentType.json
      }
    }
  )
}
/** 点评回复 回复他人的评论*/
export function replyComment(params: IApiRelative.IReplyComment.IParams) {
  return httpPost<IApiRelative.IGetNoteComments.IGetNoteCommentItem>("/my/comment-reply", params, {
    header: {
      "content-type": IContentType.json
    }
  })
}
/** 评论点赞 取消点赞 */
export function switchCommentLike(params: /** 评论id*/ PrimaryKey<number | string>) {
  return httpPost<IApiRelative.ISwitchCommentLike.IResData>("/my/like-comment/" + params.id)
}
/** 笔记收藏、取消收藏 */
export function switchNoteCollect(params: /** 笔记id*/ PrimaryKey<number | string>) {
  return httpPost<unknown>("/my/note-collect/" + params.id)
}
/** 评论举报 */
export function reportComment(
  params: PrimaryKey<number | string>,
  data: IApiRelative.IReportComment.IParams
) {
  return httpPost<unknown>("/my/report-comment/" + params.id, data, {
    header: {
      "content-type": IContentType.json
    }
  })
}
/** 小程序用户发布笔记 */
export function publishNote(params: IApiRelative.IPublishNote.IParams) {
  return httpPost<unknown>("/community/note", params, {
    header: {
      "content-type": IContentType.json
    }
  })
}
/** 话题快速搜索 */
export function quickSearchTopic(params: IApiRelative.IQuickSearchTopic.IParams) {
  return httpPost<IApiRelative.IQuickSearchTopic.IResData>("/community/quick-discussions", params, {
    header: {
      "content-type": IContentType.json
    }
  })
}
/** 内容专题详情 */
export function getContentSpecialDetail(params: PrimaryKey<number | string>) {
  return httpGet<IApiRelative.IGetContentSpecialDetail.IResData>("/content-topic/" + params.id)
}
/** 专业百科详情 */
export function getBaikeDetail(params: PrimaryKey<number | string>) {
  return httpGet<IApiRelative.IGetBaikeDetail.IResData>("/content-encyclopedia/" + params.id)
}
/** 话题收藏、取消话题收藏 */
export function switchWordCollect(/** 话题id */ params: PrimaryKey<number | string>) {
  return httpPost<unknown>("/my/discussion-collect/" + params.id)
}
/** 用户资料更新(头像昵称) */
export function updateUserAvatarAndNickname(
  params: IApiRelative.IUpdateUserAvatarAndNickname.IParams
) {
  return httpPut<IApiRelative.IUpdateUserAvatarAndNickname.IResData>("/my/head", params, {
    header: {
      "content-type": IContentType.json
    }
  })
}
/** 笔记标签详情 */
export function getNoteTagDetail(params: /** 标签id */ PrimaryKey<number | string>) {
  return httpGet<IApiRelative.IGetNoteTagDetail.IResData>("/community/tag/" + params.id)
}
/** 笔记标签详情 笔记列表 */
export function getNoteTagDetailNoteList(
  params: /** 标签id */ PrimaryKey<number | string>,
  data: IApiRelative.IGetNoteTagDetailNoteList.IParams
) {
  return httpPost<IApiRelative.IGetNoteTagDetailNoteList.IResData>(
    "/community/tag-notes/" + params.id,
    data,
    {
      header: {
        "content-type": IContentType.json
      }
    }
  )
}
/** 我的关注 */
export function getMyConcern(params: IApiRelative.IGetMyConcern.IParams) {
  return httpPost<IApiRelative.IGetMyConcern.IResData>("/my/followings", params, {
    header: {
      "content-type": IContentType.json
    }
  })
}
/** 我的关注 访客关注、取消关注
 * @description 用于个人中心 我的关注
 */
export function switchConcern(params: { followingMiniVisitorId: number }) {
  return httpPut<unknown>("/my/following/" + params.followingMiniVisitorId)
}
/** 我的关注 访客关注、置顶 */
export function topConcern(params: { followingMiniVisitorId: number }) {
  return httpPut<unknown>("/my/following-top/" + params.followingMiniVisitorId)
}
/** 我的关注 访客关注、取消置顶 */
export function untopConcern(params: { followingMiniVisitorId: number }) {
  return httpPut<unknown>("/my/following-untop/" + params.followingMiniVisitorId)
}
/** 我的回复 */
export function getMyReply(params: IApiRelative.IGetMyReply.IParams) {
  return httpPost<IApiRelative.IGetMyReply.IResData>("/my/notifications", params, {
    header: {
      "content-type": IContentType.json
    }
  })
}
/** 我的回复 删除 */
export function deleteMyReply(params: PrimaryKey<number | string>) {
  return httpDelete<unknown>("/my/notification/" + params.id)
}
/** 我的粉丝 */
export function getMyFans(params: IApiRelative.IGetMyFans.IParams) {
  return httpPost<IApiRelative.IGetMyFans.IResData>("/my/followers", params, {
    header: {
      "content-type": IContentType.json
    }
  })
}
/** 我的话题 */
export function getMyTopics(params: IApiRelative.IGetMyTopics.IParams) {
  return httpPost<IApiRelative.IGetMyTopics.IResData>("/my/discussions", params, {
    header: {
      "content-type": IContentType.json
    }
  })
}
/** 访客主页 获取主页信息(个人主页、他人主页) */
export function getPersonalPageInfo(params: IApiRelative.IGetPersonalPageInfo.IParams) {
  return httpGet<IApiRelative.IGetPersonalPageInfo.IResData>("/home/visitor-index", params)
}
/** 个人主页 笔记列表(最热，最新，我的) */
export function getPersonalPageNoteList(
  params: IApiRelative.IGetPersonalPageNoteList.IParams,
  data: IApiRelative.IGetPersonalPageNoteList.IData
) {
  return httpPost<IApiRelative.IGetPersonalPageNoteList.IResData>(
    "/home/visitor-notes?miniVisitorId=" + params.miniVisitorId,
    data,
    {
      header: {
        "content-type": IContentType.json
      }
    }
  )
}
/** 个人主页 我收藏的笔记列表 */
export function getPersonalPageCollectedNoteList(
  data: IApiRelative.IGetPersonalPageCollectedNoteList.IData
) {
  return httpPost<IApiRelative.IGetPersonalPageCollectedNoteList.IResData>(
    "/home/visitor-collecting-notes",
    data,
    {
      header: {
        "content-type": IContentType.json
      }
    }
  )
}
/** 标签快速搜索 */
export function quickSearchTags(params: IApiRelative.IQuickSearchTags.IParams) {
  return httpPost<IApiRelative.IQuickSearchTags.IResData>("/community/quick-tags", params, {
    header: {
      "content-type": IContentType.json
    }
  })
}
/** 我的笔记删除(下架) */
export function removeMyNote(params: PrimaryKey<number>) {
  return httpDelete<unknown>("/my/note/" + params.id)
}
/** 小程序用户修改笔记 */
export function modifyMyNote(
  params: PrimaryKey<string | number>,
  data: IApiRelative.IModifyMyNote.IData
) {
  return httpPut<unknown>("/community/note/" + params.id, data)
}
